<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

use app\massage\model\Banner;
use app\massage\model\Car;
use app\massage\model\CateList;
use app\massage\model\Coach;
use app\massage\model\ConfigSetting;
use app\massage\model\CouponRecord;
use app\massage\model\Service;
use longbingcore\heepay\WeixinPay;
use think\App;
use think\facade\Db;

class Index extends \app\ApiRest
{
    protected $model;
    protected $article_model;
    protected $coach_model;
    protected $banner_model;
    protected $car_model;
    protected $admin_model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new Service();
        $this->banner_model = new Banner();
        $this->car_model = new Car();
        $this->coach_model = new Coach();
        $this->admin_model = new \app\massage\model\Admin();
    }

    public function index()
    {
        $input = $this->_param;
        $key = 'index_cach_index';
        $data = getCache($key, $this->_uniacid);
        if (empty($data)) {
            $dis = [
                'uniacid' => $this->_uniacid,
                'status' => 1
            ];
            $data['banner'] = $this->banner_model->where($dis)->field('id,img,link,type_id,connect_type')->order('top desc,id desc')->select()->toArray();
            $cate_model = new CateList();
            $data['service_cate'] = $cate_model->where($dis)->field('title,id,cover')->order('top desc,id desc')->select()->toArray();
            //判断插件权限没有返回空
            $auth = $this->getAuthList((int)$this->_uniacid, ['recommend']);
            $config_model = new ConfigSetting();
            $config = $config_model->dataInfo($this->_uniacid, ['recommend_style', 'coach_apply_show', 'auto_recommend', 'popup_img']);
            $data = array_merge($data, $config);
            $data['recommend_auth'] = $auth['recommend'];
            setCache($key, $data, 5, $this->_uniacid);
        }
        //是否有优惠券要领取
        if (!empty($this->getUserId())) {
            $data['is_pop_img'] = isset($this->_user['is_pop_img']) ? $this->_user['is_pop_img'] : 1;
            $coupon_record_model = new CouponRecord();
            $have_get = $coupon_record_model->where(['user_id' => $this->getUserId()])->column('coupon_id');
            $dis = [];
            $dis[] = ['uniacid', '=', $this->_uniacid];
            $dis[] = ['send_type', '=', 2];
            $dis[] = ['status', '=', 1];
            $dis[] = ['stock', '>', 0];
            $dis[] = ['id', 'not in', $have_get];
            $time = strtotime(date('Y-m-d', time()));
            //不是新用户
            if ($this->_user['create_time'] < $time) {
                $dis[] = ['user_limit', '<>', 2];
            }
            $map[] = ['time_limit', '=', 1];
            $map[] = ['end_time', '>', time()];
            $coupon_count = Db::name('massage_service_coupon')->where($dis)->where(function ($query) use ($map) {
                $query->whereOr($map);
            })->count();
        }
        $data['have_coupon'] = !empty($coupon_count) ? 1 : 0;
        return $this->success($data);
    }

    public function updateUserPopImg()
    {
        $input = $this->_input;
        $user_model = new \app\massage\model\User();
        $user_model->where(["id" => $this->_user["id"]])->update(["is_pop_img" => $input["is_pop_img"]]);
        $this->_user["is_pop_img"] = $input["is_pop_img"];
        $key = $this->autograph;
        setCache($key, $this->_user, 259200, 0);
        return $this->success(true);
    }

    public function recommendList()
    {
        $input = $this->_param;
        $lat = !empty($input["lat"]) ? $input["lat"] : 0;
        $lng = !empty($input["lng"]) ? $input["lng"] : 0;
        $key = "recommend_key-" . $lat . "-" . $lng;
        $list = getCache($key, $this->_uniacid);
        if (empty($list)) {
            $config_model = new \app\massage\model\ConfigSetting();
            $config = $config_model->dataInfo($this->_uniacid, ["recommend_style", "coach_apply_show", "auto_recommend"]);
            $where[] = ["uniacid", "=", $this->_uniacid];
            $where[] = ["status", "=", 2];
            $where[] = ["auth_status", "=", 2];
            $where[] = ["is_work", "=", 1];
            if (!empty($input["city_id"])) {
            }
            if ($this->is_app == 0 && getConfigSetting($this->_uniacid, "shield_massage") == 1) {
                $where[] = ["industry_type", "<>", 1];
            }
            if ($this->getUserId()) {
                $shield_coach = $this->coach_model->getShieldCoach($this->getUserId());
                if (!empty($shield_coach)) {
                    $where[] = ["id", "not in", $shield_coach];
                }
            }
            $alh = "ACOS(SIN((" . $lat . " * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((" . $lat . " * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((" . $lng . " * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6378.137*1000 as distance";
            if ($config["auto_recommend"] == 0) {
                $list = $this->coach_model->coachRecommendSelect($where, $alh);
            } else {
                $list = $this->coach_model->aotuCoachRecommendSelect($where, $alh);
            }
            if (!empty($list)) {
                $seven = $this->model->getSaleTopSeven($this->_uniacid);
                foreach ($list as &$v) {
                    $v["id"] = $v["coach_id"];
                    $v["is_new"] = in_array($v["id"], $seven) ? 1 : 0;
                    $v["star"] = number_format($v["star"], 1);
                }
            }
            if (!empty($input["diy_type"]) && $input["diy_type"] == 3) {
                $list = $this->coach_model->getCoachListWorkStatus($list, $this->_uniacid);
            }
            setCache($key, $list, 5, $this->_uniacid);
        }
        return $this->success($list);
    }

    public function serviceList()
    {
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 1];
        $dis[] = ["type", "=", 1];
        $dis[] = ["check_status", "=", 2];
        $dis[] = ["is_add", "=", 0];
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int)$this->_uniacid, ["store"]);
        if (empty($auth["store"])) {
            $dis[] = ["is_door", "=", 1];
        }
        if (!empty($input["cate_id"])) {
            $cate_model = new \app\massage\model\CateConnect();
            $id = $cate_model->where(["cate_id" => $input["cate_id"]])->column("service_id");
            $dis[] = ["id", "in", $id];
        }
        if (!empty($input["name"])) {
            $dis[] = ["title", "like", "%" . $input["name"] . "%"];
        }
        if (!empty($input["title"])) {
            $dis[] = ["title", "like", "%" . $input["title"] . "%"];
        }
        if (!empty($input["industry_id"])) {
            $dis[] = ["industry_type", "=", $input["industry_id"]];
        }
        if ($this->is_app == 0 && getConfigSetting($this->_uniacid, "shield_massage") == 1) {
            $dis[] = ["industry_type", "<>", 1];
        }
        $page = !empty($input["page"]) ? $input["page"] : 1;
        $input["sort"] = !empty($input["sort"]) ? $input["sort"] : "top desc";
        $key = serialize($dis) . "serviceList" . $input["sort"] . $page;
        $data = getCache($key, $this->_uniacid);
        if (empty($data)) {
            $data = $this->model->indexDataList($dis, 10, $input["sort"]);
            setCache($key, $data, 5, $this->_uniacid);
        }
        $data["data"] = $this->model->giveListMemberInfo($data["data"], $this->_uniacid, $this->getUserId());
        $data["data"] = giveMemberPrice($this->_uniacid, $data["data"]);
        return $this->success($data);
    }

    public function getCoachService()
    {
        $input = $this->_param;
        if (!empty($input["city_id"])) {
            $city_id = $city = $input["city_id"];
        } else {
            $lat = $input["lat"];
            $lng = $input["lng"];
            $city = $this->getLatData($lng, $lat, $this->_uniacid);
            $city_model = new \app\massage\model\City();
            $mapor = ["city_type" => 1, "is_city" => 1];
            $city_id = $city_model->where(["uniacid" => $this->_uniacid, "status" => 1])->where(function ($query) use ($mapor) {
                $query->whereOr($mapor);
            })->where("title", "in", $city)->order("id desc")->value("id");
        }
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.status", "=", 1];
        $dis[] = ["c.status", "=", 2];
        $dis[] = ["c.auth_status", "=", 2];
        if (!empty($input["industry_id"])) {
            $dis[] = ["c.industry_type", "=", $input["industry_id"]];
        }
        if ($this->is_app == 0 && getConfigSetting($this->_uniacid, "shield_massage") == 1) {
            $dis[] = ["c.industry_type", "<>", 1];
        }
        if (!empty($city)) {
            $dis[] = ["c.city_id", "=", $city_id];
        }
        if (!empty($input["title"])) {
            $dis[] = ["a.title", "like", "%" . $input["title"] . "%"];
        }
        $dis[] = ["a.check_status", "=", 2];
        if (!empty($input["coach_id"])) {
            $dis[] = ["b.coach_id", "=", $input["coach_id"]];
        }
        if (!empty($input["cate_id"])) {
            $cate_model = new \app\massage\model\CateConnect();
            $service_id = $cate_model->getCateService($input["cate_id"]);
            $dis[] = ["a.id", "in", $service_id];
        }
        $is_add = !empty($input["is_add"]) ? $input["is_add"] : 0;
        $dis[] = ["a.is_add", "=", $is_add];
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int)$this->_uniacid, ["store"]);
        if (empty($auth["store"])) {
            $dis[] = ["a.is_door", "=", 1];
        }
        $key = serialize($dis) . $input["sort"] . $input["page"];
        $data = getCache($key, $this->_uniacid);
        if (empty($data)) {
            $data = $this->model->serviceCoachPageList($dis, $input["sort"]);
            setCache($key, $data, 5, $this->_uniacid);
        }
        $data["data"] = $this->model->giveListMemberInfo($data["data"], $this->_uniacid, $this->getUserId());
        $data["data"] = giveMemberPrice($this->_uniacid, $data["data"]);
        return $this->success($data);
    }

    public function industryServiceList()
    {
        $dis = ["uniacid" => $this->_uniacid, "status" => 1];
        $type_model = new \app\industrytype\model\Type();
        if ($this->is_app == 0 && getConfigSetting($this->_uniacid, "shield_massage") == 1) {
            $wheres[] = ["type", "<>", 1];
        } else {
            $wheres = [];
        }
        $data = $type_model->where($dis)->where($wheres)->order("top desc,id desc")->field("cate_name,id")->select()->toArray();
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int)$this->_uniacid, ["store"]);
        $dis = ["uniacid" => $this->_uniacid, "status" => 1, "type" => 1, "check_status" => 2, "is_add" => 0, "index_show" => 1];
        if (empty($auth["store"])) {
            $dis["is_door"] = 1;
        }
        $arr = [];
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $dis["industry_type"] = $v["id"];
                $list = $this->model->where($dis)->field("id,show_unit,cover,title,ROUND(price+material_price,2) as price")->order("top desc,id desc")->limit(6)->select()->toArray();
                if (!empty($list)) {
                    $arr[$k]["cate_name"] = $v["cate_name"];
                    $arr[$k]["id"] = $v["id"];
                    $arr[$k]["list"] = $list;
                }
            }
        }
        $total["list"] = array_values($arr);
        return $this->success($total);
    }

    public function industryGetCoachService()
    {
        $input = $this->_param;
        if (!empty($input["city_id"])) {
            $city_id = $city = $input["city_id"];
        } else {
            $lat = $input["lat"];
            $lng = $input["lng"];
            $city = $this->getLatData($lng, $lat, $this->_uniacid);
            $city_model = new \app\massage\model\City();
            $mapor = ["city_type" => 1, "is_city" => 1];
            $city_id = $city_model->where(["uniacid" => $this->_uniacid, "status" => 1])->where(function ($query) use ($mapor) {
                $query->whereOr($mapor);
            })->where("title", "in", $city)->order("id desc")->value("id");
        }
        $type_model = new \app\industrytype\model\Type();
        $dis = ["uniacid" => $this->_uniacid, "status" => 1];
        if ($this->is_app == 0 && getConfigSetting($this->_uniacid, "shield_massage") == 1) {
            $wheres[] = ["type", "<>", 1];
        } else {
            $wheres = [];
        }
        $data = $type_model->where($dis)->where($wheres)->order("top desc,id desc")->field("cate_name,id")->select()->toArray();
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int)$this->_uniacid, ["store"]);
        $dis = ["a.uniacid" => $this->_uniacid, "a.status" => 1, "a.check_status" => 2, "a.is_add" => 0, "a.index_show" => 1];
        if (empty($auth["store"])) {
            $dis["a.is_door"] = 1;
        }
        if (!empty($city)) {
            $dis["c.city_id"] = $city_id;
        }
        $arr = [];
        if (!empty($data)) {
            foreach ($data as $k => $v) {
                $dis["a.industry_type"] = $v["id"];
                $list = $this->model->alias("a")->join("massage_service_service_coach b", "a.id = b.ser_id")->join("massage_service_coach_list c", "b.coach_id = c.id")->where($dis)->field("a.id,a.show_unit,a.cover,a.title,ROUND(a.price+a.material_price,2) as price")->group("a.id")->order("a.type desc,a.top desc,a.id desc")->limit(6)->select()->toArray();
                if (!empty($list)) {
                    $arr[$k]["cate_name"] = $v["cate_name"];
                    $arr[$k]["list"] = $list;
                    $arr[$k]["id"] = $v["id"];
                }
            }
        }
        $total["list"] = array_values($arr);
        return $this->success($total);
    }

    public function serviceInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $data = $this->model->dataInfo($dis);
        $data["price"] = round($data["price"] + $data["material_price"], 2);
        $member_service = new \app\member\model\Service();
        $user_member = $member_service->getUserMember($this->_uniacid, $this->getUserId());
        $data["member_service"] = $user_member["member_auth"] == 1 ? $data["member_service"] : 0;
        $data["member_info"] = $member_service->getServiceMember($data["id"], $user_member["member_level"], $data["member_service"]);
        $position_model = new \app\massage\model\ServicePositionConnect();
        $position = $position_model->positionInfo($data["id"]);
        $data["position_data"] = $position;
        $guarantee_model = new \app\massage\model\ServiceGuaranteeConnect();
        $data["guarantee_data"] = $guarantee_model->guaranteeInfo($data["id"]);
        $member_auth = memberDiscountAuth($this->_uniacid);
        if ($member_auth["status"] == 1) {
            $data["member_price"] = round($data["price"] * $member_auth["discount"] / 10, 2);
            $data["member_status"] = 0;
            $data["member_discount"] = $member_auth["discount"];
            $data["member_discount_price"] = round($data["price"] - $data["member_price"], 2);
            if ($this->getUserId()) {
                $user_model = new \app\massage\model\User();
                $member_discount_time = $user_model->where(["id" => $this->getUserId()])->value("member_discount_time");
                if (time() <= $member_discount_time) {
                    $data["member_status"] = 1;
                }
            }
        }
        return $this->success($data);
    }

    public function configInfo()
    {
        $input = $this->_param;
        $rules_status = !empty($input["rules_status"]) ? $input["rules_status"] : 0;
        $key = "key_config_key" . $rules_status;
        $config = getCache($key, $this->_uniacid);
        if (empty($config)) {
            $dis = ["uniacid" => $this->_uniacid];
            $config_model = new \app\massage\model\Config();
            $arr = "agent_article_id,bus_end_time,uniacid,appsecret,app_app_secret,appid,app_app_id,web_app_id,web_app_secret,gzh_appid,order_tmp_id,cancel_tmp_id,max_day,time_unit,service_cover_time,can_tx_time,company_pay,short_id,short_secret";
            $config = $config_model->where($dis)->withoutField($arr)->find()->toArray();
            $pay_config_model = new \app\massage\model\PayConfig();
            $pay_config = $pay_config_model->dataInfo($dis);
            $config["alipay_status"] = $pay_config["alipay_status"];
            $short_config_model = new \app\massage\model\ShortCodeConfig();
            $short_config = $short_config_model->dataInfo($dis);
            $config["short_code_status"] = $short_config["short_code_status"];
            $config["bind_phone_type"] = $short_config["bind_phone_type"];
            if (!empty($config["agent_article_id"])) {
                $article_model = new \app\massage\model\ArticleList();
                $config["agent_article_title"] = $article_model->where(["id" => $config["agent_article_id"]])->value("title");
            }
            $config_model = new \app\massage\model\ConfigSetting();
            $data = $config_model->dataInfo($this->_uniacid, ["agent_default_name", "balance_discount_status", "material_text", "personal_income_tax_text", "salesman_check_status", "pageColor", "coach_account_phone_status", "user_update_location", "broker_apply_port", "web_coach_port", "add_flow_path", "coach_app_text", "channel_menu_name", "channel_check_status", "coach_show_lable", "free_fare_distance", "free_fare_bear", "block_user_type", "hide_admin_mobile", "hide_coach_image", "coach_filter_show", "coach_apply_type", "coach_list_format", "coach_force_show", "coach_license_show", "realtime_location", "merchant_switch_show", "force_login", "tax_point", "salesman_poster", "channel_poster", "order_contact_coach", "service_recording_show", "coach_level_show", "attendant_name", "service_start_recording", "service_end_recording", "coach_apply_show", "recharge_status", "recommend_style", "coach_format", "wechat_transfer", "alipay_transfer", "under_transfer", "bank_transfer", "coach_career_show", "play_list_format", "agent_update_city", "free_fare_select"]);
            $config = array_merge($config, $data);
            $auth = \longbingcore\permissions\AdminMenu::getAuthList((int)$this->_uniacid, ["memberdiscount", "balancediscount", "industrytype", "materialshop", "couponatv", "subagent", "agentcoach", "orderradar", "heepay", "salesman", "abnormalorder", "channel", "reseller", "payreseller", "coachbroker", "channelcate", "coachport", "skillservice", "coachtravel", "member", "dynamic", "recommend", "store", "map", "recording", "adapay", "coachcredit"]);
            $setting_status = getConfigSettingArr($this->_uniacid, ["reseller_status", "channel_status", "broker_status", "salesman_status"]);
            $auth["reseller"] = !$auth["reseller"] ? false : $setting_status["reseller_status"];
            $auth["channel"] = !$auth["channel"] ? false : $setting_status["channel_status"];
            $auth["coachbroker"] = !$auth["coachbroker"] ? false : $setting_status["broker_status"];
            $auth["salesman"] = !$auth["salesman"] ? false : $setting_status["salesman_status"];
            $config_model = new \app\massage\model\MassageConfig();
            if ($auth["dynamic"]) {
                $config_data = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
                $auth["dynamic"] = !empty($config_data["dynamic_status"]) ? $auth["dynamic"] : 0;
            }
            if ($auth["adapay"]) {
                $adapay_config = new \app\adapay\model\Config();
                $config_data = $adapay_config->dataInfo(["uniacid" => $this->_uniacid]);
                $auth["adapay"] = !empty($config_data["status"]) ? $auth["adapay"] : 0;
            }
            if ($auth["heepay"] && $auth["adapay"] != 1) {
                $adapay_config = new \app\heepay\model\Config();
                $config_data = $adapay_config->dataInfo(["uniacid" => $this->_uniacid]);
                $auth["heepay"] = !empty($config_data["status"]) ? $auth["heepay"] : 0;
            }
            if ($auth["member"]) {
                $member_config = new \app\member\model\Config();
                $config_data = $member_config->dataInfo(["uniacid" => $this->_uniacid]);
                $auth["member"] = !empty($config_data["status"]) ? $auth["member"] : 0;
            }
            if ($auth["coachcredit"]) {
                $member_config = new \app\massage\model\CreditConfig();
                $config_data = $member_config->dataInfo(["uniacid" => $this->_uniacid]);
                $auth["coachcredit"] = !empty($config_data["status"]) ? $auth["coachcredit"] : 0;
            }
            if ($auth["memberdiscount"]) {
                $auth["memberdiscount"] = memberDiscountAuth($this->_uniacid)["status"];
            }
            $config["plugAuth"] = $auth;
            if (!empty($input["rules_status"])) {
                unset($config["trading_rules"]);
                unset($config["login_protocol"]);
                unset($config["information_protection"]);
            }
            $diy_model = new \app\massage\model\Diy();
            $diy_config = $diy_model->dataInfo(["uniacid" => $this->_uniacid]);
            $config["page"] = json_decode($diy_config["page"], true);
            $config["tabBar"] = json_decode($diy_config["tabbar"], true);
            $config_model = new \app\massage\model\BtnConfig();
            $config["btn_config"] = $config_model->where(["uniacid" => $this->_uniacid])->field("text,btn_color,font_color,type")->order("type,id desc")->select()->toArray();
            setCache($key, $config, 7, $this->_uniacid);
        }
        return $this->success($config);
    }

    public function coachServiceList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.status", "=", 1];
        $dis[] = ["a.check_status", "=", 2];
        if (!empty($input["coach_id"])) {
            $dis[] = ["b.coach_id", "=", $input["coach_id"]];
        }
        $is_add = !empty($input["is_add"]) ? $input["is_add"] : 0;
        $dis[] = ["a.is_add", "=", $is_add];
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int)$this->_uniacid, ["store"]);
        if (!empty($input["order_id"])) {
            $order_model = new \app\massage\model\Order();
            $store_id = $order_model->where(["id" => $input["order_id"]])->value("store_id");
            if (!empty($store_id)) {
                $dis[] = ["a.is_store", "=", 1];
            } else {
                $dis[] = ["a.is_door", "=", 1];
            }
        } elseif (empty($auth["store"])) {
            $dis[] = ["a.is_door", "=", 1];
        }
        $data["data"] = $this->model->serviceCoachList($dis);
        $data["car_count"] = 0;
        $data["car_price"] = $data["car_count"];
        if (!empty($data["data"])) {
            $car_model = new \app\massage\model\Car();
            foreach ($data["data"] as $k => &$v) {
                $v["price"] = round($v["price"] + $v["material_price"], 2);
                $dis = ["service_id" => $v["id"], "coach_id" => $input["coach_id"], "user_id" => $this->getUserId(), "status" => 1];
                if (!empty($input["order_id"])) {
                    $dis["order_id"] = $input["order_id"];
                }
                $car_info = $car_model->dataInfo($dis);
                $v["car_id"] = !empty($car_info) ? $car_info["id"] : 0;
                $v["car_num"] = !empty($car_info) ? $car_info["num"] : 0;
                if (!empty($car_info)) {
                    $data["car_price"] += $v["price"] * $v["car_num"];
                    $data["car_price"] = round($data["car_price"], 2);
                    $data["car_count"] += $v["car_num"];
                }
            }
        }
        $data["data"] = $this->model->giveListMemberInfo($data["data"], $this->_uniacid, $this->getUserId());
        $data["data"] = giveMemberPrice($this->_uniacid, $data["data"]);
        return $this->success($data);
    }

    public function serviceCoachList()
    {
        $input = $this->_param;
        $sql_type = 1;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.status", "=", 2];
        $dis[] = ["a.auth_status", "=", 2];
        $dis[] = ["a.is_work", "=", 1];
        if (!empty($input["ser_id"])) {
            $dis[] = ["b.ser_id", "=", $input["ser_id"]];
            $sql_type = 2;
        }
        if (!empty($input["coach_name"])) {
            $dis[] = ["a.coach_name", "like", "%" . $input["coach_name"] . "%"];
        }
        if (!empty($input["city_id"])) {
            $dis[] = ["a.city_id", "=", $input["city_id"]];
        }
        if (isset($input["sex"])) {
            $dis[] = ["a.sex", "=", $input["sex"]];
        }
        if (!empty($input["work_time_start"]) && !empty($input["work_time_end"])) {
            $dis[] = ["a.work_time", "between", $input["work_time_start"] . "," . $input["work_time_end"]];
        }
        $collect_model = new \app\massage\model\CoachCollect();
        $user_id = $this->getUserId() ? $this->getUserId() : 0;
        $collect = $collect_model->where(["user_id" => $user_id])->column("coach_id");
        if (!empty($input["recommend"]) || isset($input["sort"]) && $input["sort"] == 1) {
            $config_model = new \app\massage\model\ConfigSetting();
            $config = $config_model->dataInfo($this->_uniacid, ["auto_recommend"]);
            if ($config["auto_recommend"] == 0) {
                $dis[] = ["a.recommend", "=", 1];
            }
        }
        if (!empty($input["service_type"])) {
            $dis[] = ["c.status", "=", 1];
            $dis[] = ["c.is_add", "=", 0];
            if ($input["service_type"] == 1) {
                $dis[] = ["c.is_store", "=", 1];
            } else {
                $dis[] = ["c.is_door", "=", 1];
            }
            $sql_type = 2;
        }
        if (!empty($input["cate_id"])) {
            $cate_model = new \app\massage\model\CateConnect();
            $id = $cate_model->getCateServiceOnline($input["cate_id"]);
            $dis[] = ["b.ser_id", "in", $id];
            $sql_type = 2;
        }
        if (!empty($input["industry_type"])) {
            $dis[] = ["a.industry_type", "=", $input["industry_type"]];
        }
        if ($this->is_app == 0 && getConfigSetting($this->_uniacid, "shield_massage") == 1) {
            $dis[] = ["a.industry_type", "<>", 1];
        }
        if (!empty($input["not_have_play"])) {
            $dis[] = ["a.industry_type", "<>", 2];
        }
        if (!empty($input["collect"]) || isset($input["sort"]) && $input["sort"] == 6) {
            $dis[] = ["a.id", "in", $collect];
        }
        if (isset($input["sort"]) && $input["sort"] == 3) {
            $start_time = strtotime("-7 day");
            $dis[] = ["a.create_time", ">=", $start_time];
        }
        $store_model = new \app\store\model\StoreList();
        if (!empty($input["store_name"])) {
            $store_coach_id = $store_model->getStoreCoachId($input["store_name"], 2);
            $dis[] = ["a.id", "in", $store_coach_id];
        }
        $city_id = !empty($input["city_id"]) ? $input["city_id"] : 0;
        if (!empty($input["store_id"])) {
            $store_coach_id = $store_model->getStoreCoachId($input["store_id"]);
            $dis[] = ["a.id", "in", $store_coach_id];
        }
        $working_coach = $this->coach_model->getWorkingCoach($this->_uniacid);
        $cannot = \app\massage\model\CoachTimeList::getCannotCoach($this->_uniacid, 0, $city_id);
        $cannot = array_diff($cannot, $working_coach);
        if ($this->getUserId()) {
            $shield_coach = $this->coach_model->getShieldCoach($this->getUserId());
            if (!empty($input["service_time"])) {
                $working_coach = $this->coach_model->getWorkingCoach($this->_uniacid, $input["service_time"]);
                $cannot = \app\massage\model\CoachTimeList::getCannotCoach($this->_uniacid, $input["service_time"], $city_id);
                $working_coach = array_merge($working_coach, $cannot);
                $shield_coach = array_merge($working_coach, $shield_coach);
            }
            if (!empty($shield_coach)) {
                $dis[] = ["a.id", "not in", $shield_coach];
            }
        }
        if (!empty($input["type"])) {
            if ($input["type"] == 1) {
                $array = array_merge($working_coach, $cannot);
                $dis[] = ["a.id", "not in", $array];
            } elseif ($input["type"] == 2) {
                $dis[] = ["a.id", "in", $working_coach];
            } elseif ($input["type"] == 3) {
                $dis[] = ["a.id", "in", $cannot];
            }
        }
        $free_fare = !empty($input["free_fare"]) ? $input["free_fare"] : 0;
        $sort = !empty($input["sort"]) ? $input["sort"] : 0;
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int)$this->_uniacid, ["recommend", "coachcredit"]);
        $lat = !empty($input["lat"]) ? $input["lat"] : 0;
        $lng = !empty($input["lng"]) ? $input["lng"] : 0;
        $alh = "ACOS(SIN((" . $lat . " * 3.1415) / 180 ) *SIN((a.lat * 3.1415) / 180 ) +COS((" . $lat . " * 3.1415) / 180 ) * COS((a.lat * 3.1415) / 180 ) *COS((" . $lng . " * 3.1415) / 180 - (a.lng * 3.1415) / 180 ) ) * 6378.137*1000 as distance";
        $data = $this->coach_model->serviceCoachList($dis, $alh, 10, $auth["coachcredit"], $this->_uniacid, $city_id, $free_fare, $sql_type, $sort);
        if (!empty($data["data"])) {
            $config_model = new \app\massage\model\Config();
            $coach_model = new \app\massage\model\Coach();
            $store_model = new \app\massage\model\StoreList();
            $icon_model = new \app\massage\model\CoachIcon();
            $personality_icon_model = new \app\massage\model\IconCoach();
            $config = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
            $coach_icon_type = getConfigSetting($this->_uniacid, "coach_icon_type");
            $top = $this->model->getSaleTopOne($this->_uniacid, $city_id, $coach_icon_type);
            $five = $this->model->getSaleTopFive($this->_uniacid, $top, $city_id, $coach_icon_type);
            $seven = $this->model->getSaleTopSeven($this->_uniacid, $city_id, $coach_icon_type);
            $type_model = new \app\industrytype\model\Type();
            $station_model = new \app\massage\model\StationIcon();
            $type = $type_model->dataSelect(["uniacid" => $this->_uniacid], "employment_years,id");
            $station = $station_model->where(["uniacid" => $this->_uniacid, "status" => 1])->column("title", "id");
            foreach ($data["data"] as &$v) {
                $v["star"] = number_format($v["star"], 1);
                $v["free_fare"] = 0 < $v["free_fare_bear"] && 0 < $v["free_fare_distance"] && $v["distance_data"] <= $v["free_fare_distance"] * 1000 ? 1 : 0;
                if (isset($v["credit_value"])) {
                    $v["credit_value"] = floatval($v["credit_value"]);
                }
                $v["store"] = $store_model->where(["id" => $v["store_id"], "status" => 1])->field("id,title")->find();
                $v["is_collect"] = in_array($v["id"], $collect) ? 1 : 0;
                $v["near_time"] = $coach_model->getCoachEarliestTimev3($v, $config, 0, 0, 1);
                if (in_array($v["id"], $working_coach)) {
                    $text_type = 2;
                } elseif (empty($v["near_time"])) {
                    $text_type = 4;
                } elseif (!in_array($v["id"], $cannot)) {
                    $text_type = 1;
                } else {
                    $text_type = 3;
                }
                $v["text_type"] = $text_type;
                if (in_array($v["id"], $top)) {
                    $v["coach_type_status"] = 1;
                } elseif (in_array($v["id"], $five)) {
                    $v["coach_type_status"] = 2;
                } elseif (in_array($v["id"], $seven)) {
                    $v["coach_type_status"] = 3;
                } elseif ($v["recommend_icon"] == 1) {
                    $v["coach_type_status"] = 4;
                } else {
                    $v["coach_type_status"] = 0;
                }
                if ($coach_icon_type == 1) {
                    $v["coach_icon"] = $icon_model->where(["id" => $v["coach_icon"]])->value("icon");
                } else {
                    $v["coach_icon"] = "";
                }
                if (!empty($v["personality_icon"])) {
                    $v["personality_icon"] = $personality_icon_model->where(["id" => $v["personality_icon"], "status" => 1])->value("title");
                } else {
                    $v["personality_icon"] = "";
                }
                $v["year"] = !empty($v["birthday"]) ? floor((time() - $v["birthday"]) / 31536000) : 0;
                $v["industry_data"] = empty($v["industry_type"]) ? [] : $type[$v["industry_type"]];
                $v["station_icon_name"] = empty($v["station_icon"]) ? "" : (isset($station[$v["station_icon"]]) ? $station[$v["station_icon"]] : "");
            }
        }
        return $this->success($data);
    }

    public function agentInfo()
    {
        $input = $this->_param;
        $admin_model = new \app\massage\model\Admin();
        $data = $admin_model->where(["id" => $input["admin_id"]])->field("agent_name,id,license,merchant_name")->find();
        return $this->success($data);
    }

    public function typeServiceCoachList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.status", "=", 2];
        $dis[] = ["a.auth_status", "=", 2];
        $sql_type = 1;
        if (!empty($input["ser_id"])) {
            $dis[] = ["b.ser_id", "=", $input["ser_id"]];
            $sql_type = 2;
        }
        if (!empty($input["coach_name"])) {
            $dis[] = ["a.coach_name", "like", "%" . $input["coach_name"] . "%"];
        }
        $collect_model = new \app\massage\model\CoachCollect();
        $user_id = $this->getUserId() ? $this->getUserId() : 0;
        $collect = $collect_model->where(["user_id" => $user_id])->column("coach_id");
        if (!empty($input["recommend"]) || isset($input["sort"]) && $input["sort"] == 1) {
            $config_model = new \app\massage\model\ConfigSetting();
            $config = $config_model->dataInfo($this->_uniacid, ["auto_recommend"]);
            if ($config["auto_recommend"] == 0) {
                $dis[] = ["a.recommend", "=", 1];
            }
        }
        $store_model = new \app\store\model\StoreList();
        if (!empty($input["store_id"])) {
            $store_coach_id = $store_model->getStoreCoachId($input["store_id"]);
            $dis[] = ["a.id", "in", $store_coach_id];
        }
        if (!empty($input["store_name"])) {
            $store_coach_id = $store_model->getStoreCoachId($input["store_name"], 2);
            $dis[] = ["a.id", "in", $store_coach_id];
        }
        if (isset($input["sex"])) {
            $dis[] = ["a.sex", "=", $input["sex"]];
        }
        if (!empty($input["service_type"])) {
            $dis[] = ["c.is_add", "=", 0];
            $dis[] = ["c.status", "=", 1];
            if ($input["service_type"] == 1) {
                $dis[] = ["c.is_store", "=", 1];
            } else {
                $dis[] = ["c.is_door", "=", 1];
            }
            $sql_type = 2;
        }
        if (!empty($input["city_id"])) {
            $dis[] = ["a.city_id", "=", $input["city_id"]];
        }
        $city_id = !empty($input["city_id"]) ? $input["city_id"] : 0;
        if (!empty($input["work_time_start"]) && !empty($input["work_time_end"])) {
            $dis[] = ["a.work_time", "between", $input["work_time_start"] . "," . $input["work_time_end"]];
        }
        if (!empty($input["cate_id"])) {
            $cate_model = new \app\massage\model\CateConnect();
            $id = $cate_model->getCateServiceOnline($input["cate_id"]);
            $dis[] = ["b.ser_id", "in", $id];
            $sql_type = 2;
        }
        if ($this->is_app == 0 && getConfigSetting($this->_uniacid, "shield_massage") == 1) {
            $dis[] = ["a.industry_type", "<>", 1];
        }
        if (!empty($input["industry_type"])) {
            $dis[] = ["a.industry_type", "=", $input["industry_type"]];
        }
        if (!empty($input["not_have_play"])) {
            $dis[] = ["a.industry_type", "<>", 2];
        }
        if (!empty($input["collect"]) || isset($input["sort"]) && $input["sort"] == 6) {
            $dis[] = ["a.id", "in", $collect];
        }
        if (isset($input["sort"]) && $input["sort"] == 3) {
            $start_time = strtotime("-7 day");
            $dis[] = ["a.create_time", ">=", $start_time];
        }
        $this->coach_model->setIndexTopCoach($this->_uniacid);
        if ($this->getUserId()) {
            $shield_coach = $this->coach_model->getShieldCoach($this->getUserId());
            if (!empty($input["service_time"])) {
                $working_coach = $this->coach_model->getWorkingCoach($this->_uniacid, $input["service_time"]);
                $cannot = \app\massage\model\CoachTimeList::getCannotCoach($this->_uniacid, $input["service_time"]);
                $working_coach = array_merge($working_coach, $cannot);
                $shield_coach = array_merge($working_coach, $shield_coach);
            }
            if (!empty($shield_coach)) {
                $dis[] = ["a.id", "not in", $shield_coach];
            }
        }
        $free_fare = !empty($input["free_fare"]) ? $input["free_fare"] : 0;
        $sort = !empty($input["sort"]) ? $input["sort"] : 0;
        $lat = !empty($input["lat"]) ? $input["lat"] : 0;
        $lng = !empty($input["lng"]) ? $input["lng"] : 0;
        $alh = "ACOS(SIN((" . $lat . " * 3.1415) / 180 ) *SIN((a.lat * 3.1415) / 180 ) +COS((" . $lat . " * 3.1415) / 180 ) * COS((a.lat * 3.1415) / 180 ) *COS((" . $lng . " * 3.1415) / 180 - (a.lng * 3.1415) / 180 ) ) * 6378.137*1000 as distance";
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int)$this->_uniacid, ["recommend", "coachcredit"]);
        $data = $this->coach_model->typeServiceCoachList($dis, $alh, 10, $auth["coachcredit"], $this->_uniacid, $city_id, $free_fare, $sql_type, $sort);
        if (!empty($data["data"])) {
            $config_model = new \app\massage\model\Config();
            $coach_model = new \app\massage\model\Coach();
            $store_model = new \app\massage\model\StoreList();
            $icon_model = new \app\massage\model\CoachIcon();
            $personality_icon_model = new \app\massage\model\IconCoach();
            $config = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
            $coach_icon_type = getConfigSetting($this->_uniacid, "coach_icon_type");
            $top = $this->model->getSaleTopOne($this->_uniacid, $city_id, $coach_icon_type);
            $five = $this->model->getSaleTopFive($this->_uniacid, $top, $city_id, $coach_icon_type);
            $seven = $this->model->getSaleTopSeven($this->_uniacid, $city_id, $coach_icon_type);
            $type_model = new \app\industrytype\model\Type();
            $type = $type_model->dataSelect(["uniacid" => $this->_uniacid], "employment_years,id");
            $station_model = new \app\massage\model\StationIcon();
            $station = $station_model->where(["uniacid" => $this->_uniacid, "status" => 1])->column("title", "id");
            foreach ($data["data"] as &$v) {
                $v["star"] = number_format($v["star"], 1);
                $v["free_fare"] = 0 < $v["free_fare_bear"] && 0 < $v["free_fare_distance"] && $v["distance_data"] <= $v["free_fare_distance"] * 1000 ? 1 : 0;
                if (isset($v["credit_value"])) {
                    $v["credit_value"] = floatval($v["credit_value"]);
                }
                $v["store"] = $store_model->where(["id" => $v["store_id"], "status" => 1])->field("id,title")->find();
                $v["is_collect"] = in_array($v["id"], $collect) ? 1 : 0;
                $v["near_time"] = $coach_model->getCoachEarliestTimev3($v, $config, 0, 0, 1);
                if ($v["is_work"] == 0 || empty($v["near_time"])) {
                    $text_type = 4;
                } elseif ($v["index_top"] == 1) {
                    $text_type = 1;
                } else {
                    $text_type = 3;
                }
                $v["text_type"] = $text_type;
                if (in_array($v["id"], $top)) {
                    $v["coach_type_status"] = 1;
                } elseif (in_array($v["id"], $five)) {
                    $v["coach_type_status"] = 2;
                } elseif (in_array($v["id"], $seven)) {
                    $v["coach_type_status"] = 3;
                } elseif ($v["recommend_icon"] == 1) {
                    $v["coach_type_status"] = 4;
                } else {
                    $v["coach_type_status"] = 0;
                }
                if ($coach_icon_type == 1) {
                    $v["coach_icon"] = $icon_model->where(["id" => $v["coach_icon"]])->value("icon");
                } else {
                    $v["coach_icon"] = "";
                }
                if (!empty($v["personality_icon"])) {
                    $v["personality_icon"] = $personality_icon_model->where(["id" => $v["personality_icon"], "status" => 1])->value("title");
                } else {
                    $v["personality_icon"] = "";
                }
                $v["year"] = !empty($v["birthday"]) ? floor((time() - $v["birthday"]) / 31536000) : 0;
                $v["industry_data"] = empty($v["industry_type"]) ? [] : $type[$v["industry_type"]];
                $v["station_icon_name"] = empty($v["station_icon"]) ? "" : (isset($station[$v["station_icon"]]) ? $station[$v["station_icon"]] : "");
            }
        }
        return $this->success($data);
    }

    public function carInfo()
    {
        $input = $this->_param;
        $order_id = !empty($input["order_id"]) ? $input["order_id"] : 0;
        $car_info = $this->car_model->carPriceAndCount($this->getUserId(), $input["coach_id"], 1, $order_id);
        return $this->success($car_info);
    }

    public function onceMoreOrder()
    {
        $input = $this->_input;
        $order_model = new \app\massage\model\Order();
        $order = $order_model->dataInfo(["id" => $input["order_id"]]);
        $coach = $this->coach_model->dataInfo(["id" => $order["coach_id"]]);
        if ($coach["status"] != 2 || $coach["is_work"] == 0) {
            $this->errorMsg("技师未上班");
        }
        $this->car_model->where(["user_id" => $this->getUserId(), "coach_id" => $order["coach_id"]])->delete();
        \think\facade\Db::startTrans();
        foreach ($order["order_goods"] as $v) {
            $ser = $this->model->dataInfo(["id" => $v["goods_id"]]);
            if (empty($ser) || $ser["status"] != 1) {
                \think\facade\Db::rollback();
                $this->errorMsg("服务已经下架");
            }
            $dis = ["user_id" => $this->getUserId(), "uniacid" => $this->_uniacid, "coach_id" => $order["coach_id"], "service_id" => $v["goods_id"], "num" => $v["num"]];
            $res = $this->car_model->dataAdd($dis);
        }
        \think\facade\Db::commit();
        return $this->success($res);
    }

    public function addCar()
    {
        $input = $this->_input;
        $order_id = !empty($input["order_id"]) ? $input["order_id"] : 0;
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "coach_id" => $input["coach_id"], "service_id" => $input["service_id"], "order_id" => $order_id];
        if (!empty($order_id)) {
        }
        if (!empty($input["coach_service"])) {
            $this->car_model->where(["coach_id" => $input["coach_id"]])->delete();
        }
        if (isset($input["num"]) && $input["num"] <= 0) {
            $this->errorMsg("数据错误");
        }
        $info = $this->car_model->dataInfo($insert);
        if (!empty($info)) {
            if (!empty($input["is_top"])) {
                return $this->success(1);
            }
            $res = $this->car_model->dataUpdate(["id" => $info["id"]], ["num" => $info["num"] + $input["num"]]);
            return $this->success($res);
        }
        $insert["num"] = $input["num"];
        $insert["status"] = 1;
        $res = $this->car_model->dataAdd($insert);
        $id = $this->car_model->getLastInsID();
        return $this->success($id);
    }

    public function delCar()
    {
        $input = $this->_input;
        $info = $this->car_model->dataInfo(["id" => $input["id"]]);
        if (!empty($info) && $input["num"] < $info["num"]) {
            $res = $this->car_model->dataUpdate(["id" => $info["id"]], ["num" => $info["num"] - $input["num"]]);
        } else {
            $res = $this->car_model->where(["id" => $info["id"]])->delete();
        }
        return $this->success($res);
    }

    public function carUpdate()
    {
        $input = $this->_input;
        $res = $this->car_model->where("id", "in", $input["id"])->update(["status" => $input["status"]]);
        return $this->success($res);
    }

    public function delSomeCar()
    {
        $input = $this->_input;
        $dis = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "coach_id" => $input["coach_id"]];
        $res = $this->car_model->where($dis)->delete();
        return $this->success($res);
    }

    public function commentList()
    {
        $input = $this->_param;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.status", "=", 1];
        if (!empty($input["coach_id"])) {
            $dis[] = ["d.id", "=", $input["coach_id"]];
        }
        if (!empty($input["coach_name"])) {
            $dis[] = ["d.coach_name", "like", "%" . $input["coach_name"] . "%"];
        }
        if (!empty($input["goods_name"])) {
            $dis[] = ["c.goods_name", "like", "%" . $input["goods_name"] . "%"];
        }
        $comment_model = new \app\massage\model\Comment();
        $config_model = new \app\massage\model\Config();
        $data = $comment_model->dataList($dis);
        $anonymous_evaluate = $config_model->where(["uniacid" => $this->_uniacid])->value("anonymous_evaluate");
        if (!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
                if ($anonymous_evaluate == 1 || $v["user_id"] == 0) {
                    $v["nickName"] = "匿名用户";
                    $v["avatarUrl"] = "https://lbqny.migugu.com/admin/farm/default-user.png";
                }
            }
        }
        return $this->success($data);
    }

    public function coachInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $data = $this->coach_model->where($dis)->withoutField("id_card,mobile,service_price,car_price")->find()->toArray();
        $length = strlen($data["id_code"]);
        $masked = 0 < $length ? substr_replace($data["id_code"], str_repeat("*", $length - 3), 3, $length - 3) : "";
        $data["id_code"] = $masked;
        $user_model = new \app\massage\model\User();
        $data["nickName"] = $user_model->where(["id" => $data["user_id"]])->value("nickName");
        $city_model = new \app\massage\model\City();
        $data["city"] = $city_model->where(["id" => $data["city_id"]])->value("title");
        $shield_model = new \app\massage\model\ShieldList();
        $shield = $shield_model->where(["user_id" => $this->_user["id"], "coach_id" => $data["id"]])->where("type", "in", [2, 3])->find();
        $data["is_shield"] = !empty($shield) ? 1 : 0;
        $store_model = new \app\massage\model\StoreList();
        $data["store_name"] = $store_model->where(["id" => $data["store_id"], "status" => 1])->value("title");
        $config_model = new \app\massage\model\Config();
        $config = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
        $data["near_time"] = $this->coach_model->getCoachEarliestTimev3($data, $config);
        $data["text_type"] = $this->coach_model->getCoachWorkStatus($data["id"], $this->_uniacid);
        $data["year"] = !empty($data["birthday"]) ? floor((time() - $data["birthday"]) / 31536000) : 0;
        $data["order_num"] += $data["total_order_num"];
        $record_model = new \app\massage\model\CreditRecord();
        $data["credit_value"] = $record_model->getSingleCoachValue($this->_uniacid, $input["id"]);
        $where = ["uniacid" => $this->_uniacid, "coach_id" => $data["id"], "user_id" => $this->getUserId()];
        $collect_model = new \app\massage\model\CoachCollect();
        $res = $collect_model->dataInfo($where);
        $data["is_collect"] = !empty($res) ? 1 : 0;
        $type_model = new \app\industrytype\model\Type();
        $data["industry_data"] = $type_model->dataInfo(["id" => $data["industry_type"]]);
        $dy_model = new \app\dynamic\model\DynamicThumbs();
        $data["dynamic_thumbs_num"] = $dy_model->where(["coach_id" => $data["id"], "status" => 1])->count();
        $follow_model = new \app\dynamic\model\DynamicFollow();
        $follow = $follow_model->dataInfo(["coach_id" => $input["id"], "user_id" => $this->getUserId(), "status" => 1]);
        $data["is_follow"] = empty($follow) ? 0 : 1;
        $data["follow_num"] = $follow_model->where(["coach_id" => $data["id"], "status" => 1])->count();
        $station_model = new \app\massage\model\StationIcon();
        $station_icon = $station_model->where(["uniacid" => $this->_uniacid, "status" => 1, "id" => $data["station_icon"]])->value("title");
        $data["station_icon_name"] = empty($station_icon) ? "" : $station_icon;
        $personality_icon_model = new \app\massage\model\IconCoach();
        if (!empty($data["personality_icon"])) {
            $data["personality_icon"] = $personality_icon_model->where(["id" => $data["personality_icon"], "status" => 1])->value("title");
        } else {
            $data["personality_icon"] = "";
        }
        $life_icon = getConfigSettingArr($this->_uniacid, ["life_icon_status", "life_icon_text"]);
        $data = array_merge($data, $life_icon);
        $this->coach_model->where(["id" => $input["id"]])->update(["pv" => \think\facade\Db::Raw("pv+1")]);
        $data["star"] = number_format($data["star"], 1);
        return $this->success($data);
    }

    public function getMapInfo()
    {
        $input = $this->_param;
        $key = $input["location"];
        $data = getCache($key, $this->_uniacid);
        if (empty($data)) {
            $map_key_key = getCache("map_key_key", $this->_uniacid);
            if (empty($map_key_key)) {
                $config_model = new \app\massage\model\Config();
                $config = $config_model->getCacheInfo($this->_uniacid);
                $map_key_key = getConfigSetting($this->_uniacid, "tencent_map_key");
                $map_key_key = !empty($map_key_key) ? explode(",", $map_key_key) : [];
                if (!empty($map_key_key)) {
                    array_unshift($map_key_key, $config["map_secret"]);
                } else {
                    $map_key_key[] = $config["map_secret"];
                }
                setCache("map_key_key", $map_key_key, 86400, $this->_uniacid);
            }
            foreach ($map_key_key as $k => $value) {
                $url = "https://apis.map.qq.com/ws/geocoder/v1/?location=";
                $url = $url . $input["location"] . "&key=" . $value;
                $data = longbingCurl($url, []);
                $data_arr = json_decode($data, true);
                if (isset($data_arr["status"]) && $data_arr["status"] == 0) {
                    setCache($key, $data, 300, $this->_uniacid);
                    if ($k != 0) {
                        unset($map_key_key[$k]);
                        array_unshift($map_key_key, $value);
                        setCache("map_key_key", $map_key_key, 86400, $this->_uniacid);
                    }
                } else {
                    $data = "{\"status\":1,\"result\":{}}";
                }
            }
        }
        return $this->success($data);
    }

    public function nearbyLocation()
    {
        $input = $this->_param;
        $key = "nearbyLocation";
        $map_key_key = getCache($key, $this->_uniacid);
        if (empty($map_key_key)) {
            $config_model = new \app\massage\model\Config();
            $config = $config_model->getCacheInfo($this->_uniacid);
            $map_key_key = getConfigSetting($this->_uniacid, "tencent_map_key");
            $map_key_key = !empty($map_key_key) ? explode(",", $map_key_key) : [];
            array_unshift($map_key_key, $config["map_secret"]);
            setCache($key, $map_key_key, 86400, $this->_uniacid);
        }
        foreach ($map_key_key as $k => $value) {
            $url = "https://apis.map.qq.com/ws/place/v1/search?boundary=" . $input["boundary"] . "&key=" . $value . "&keyword=" . $input["keyword"] . "&page_size=20&page_index=" . $input["page"];
            $data = longbingCurl($url, []);
            $data_arr = json_decode($data, true);
            if (isset($data_arr["status"]) && $data_arr["status"] == 0) {
                if ($k != 0) {
                    unset($map_key_key[$k]);
                    array_unshift($map_key_key, $value);
                    setCache($key, $map_key_key, 86400, $this->_uniacid);
                }
                return $this->success($data);
            }
            $data = "{\"status\":1,\"result\":{}}";
        }
    }

    public function getCity()
    {
        $input = $this->_param;
        $city_model = new \app\massage\model\City();
        $lat = $input["lat"];
        $lng = $input["lng"];
        $alh = "ACOS(SIN((" . $lat . " * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((" . $lat . " * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((" . $lng . " * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6378.137*1000 as distance";
        $key_find = getConfigSetting($this->_uniacid, "index_city_find");
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 1];
        $admin_id = !empty($input["admin_id"]) ? $input["admin_id"] : 0;
        $agent_update_city = getConfigSetting($this->_uniacid, "agent_update_city");
        if (!empty($input["admin_id"]) && $agent_update_city == 0) {
            $admin_model = new \app\massage\model\Admin();
            $admin = $admin_model->dataInfo(["id" => $input["admin_id"]]);
            if (!empty($admin)) {
                if ($admin["city_type"] == 3) {
                    $dis[] = ["pid", "=", $admin["city_id"]];
                } elseif ($admin["city_type"] == 1) {
                    $dis[] = ["id", "=", $admin["city_id"]];
                } else {
                    $city = $city_model->dataInfo(["id" => $admin["city_id"]]);
                    if (!empty($city)) {
                        if ($city["is_city"] == 1) {
                            $dis[] = ["id", "=", $admin["city_id"]];
                        } else {
                            $dis[] = ["id", "=", $city["pid"]];
                        }
                    }
                }
            }
        }
        $mapor = ["city_type" => 1, "is_city" => 1];
        $key = "getCity_getCityss" . round($lng, 4) . "-" . round($lat, 4) . "-" . $key_find . $admin_id . $agent_update_city;
        $data = getCache($key, $this->_uniacid);
        if (!empty($data)) {
            return $this->success($data);
        }
        if ($key_find == 1) {
            $data = $city_model->where($dis)->where(function ($query) use ($mapor) {
                $query->whereOr($mapor);
            })->field(["id,title,lat,lng,city_type,if(city_type=1,id,pid) as top", $alh])->order("city_type desc,id desc")->select()->toArray();
            $city = $this->getLatData($lng, $lat, $this->_uniacid);
            if (!empty($data)) {
                foreach ($data as &$v) {
                    if (is_array($city) && in_array($v["title"], $city)) {
                        $v["is_select"] = 1;
                    }
                }
            }
        } else {
            $data = $city_model->where($dis)->where(function ($query) use ($mapor) {
                $query->whereOr($mapor);
            })->field(["id,title,lat,lng,city_type,if(city_type=1,id,pid) as top", $alh])->order("distance asc,id desc")->select()->toArray();
            $data[0]["is_select"] = 1;
        }
        $data = $this->cityTop($data);
        setCache($key, $data, 3600, $this->_uniacid, "city_key");
        return $this->success($data);
    }

    public function cityTop($arr)
    {
        if (empty($arr)) {
            return $arr;
        }
        $new_arr = [];
        foreach ($arr as $value) {
            $new_arr[$value["top"]] = !empty($new_arr[$value["top"]]) ? $new_arr[$value["top"]] : [];
            if ($value["city_type"] == 1) {
                array_unshift($new_arr[$value["top"]], $value);
            } else {
                array_push($new_arr[$value["top"]], $value);
            }
        }
        $total = [];
        foreach ($new_arr as $value) {
            foreach ($value as $values) {
                $total[] = $values;
            }
        }
        return $total;
    }

    public function couponList()
    {
        if (!$this->getUserId()) {
            return $this->success([]);
        }
        $input = $this->_param;
        $coupon_record_model = new \app\massage\model\CouponRecord();
        $have_get = $coupon_record_model->where(["user_id" => $this->getUserId()])->column("coupon_id");
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.send_type", "=", 2];
        $dis[] = ["a.status", "=", 1];
        $dis[] = ["a.stock", ">", 0];
        $dis[] = ["a.id", "not in", $have_get];
        $time = strtotime(date("Y-m-d", time()));
        if ($this->_user["create_time"] < $time) {
            $dis[] = ["a.user_limit", "<>", 2];
        }
        $agent_coupon_location = getConfigSetting($this->_uniacid, "agent_coupon_location");
        $where = [];
        if (isset($input["lng"]) && $agent_coupon_location == 1) {
            $city = getCityByLat($input["lng"], $input["lat"], $this->_uniacid);
            $city = array_values($city);
            $where[] = ["a.admin_id", "=", 0];
            $where[] = ["c.title", "in", $city];
        }
        $data = \think\facade\Db::name("massage_service_coupon")->alias("a")->join("shequshop_school_admin b", "a.admin_id = b.id", "left")->join("massage_service_city_list c", "b.city_id = c.id", "left")->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->field("a.id,a.title,a.user_limit,a.full,a.discount")->group("a.id")->select();
        return $this->success($data);
    }

    public function couponListV2()
    {
        if (!$this->getUserId()) {
            return $this->success([]);
        }
        $input = $this->_param;
        $coupon_record_model = new \app\massage\model\CouponRecord();
        $have_get = $coupon_record_model->where(["user_id" => $this->getUserId()])->column("coupon_id");
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.send_type", "=", 2];
        $dis[] = ["a.status", "=", 1];
        $dis[] = ["a.stock", ">", 0];
        $dis[] = ["a.id", "not in", $have_get];
        $time = strtotime(date("Y-m-d", time()));
        if ($this->_user["create_time"] < $time) {
            $dis[] = ["a.user_limit", "<>", 2];
        }
        $agent_coupon_location = getConfigSetting($this->_uniacid, "agent_coupon_location");
        $where = [];
        if ($agent_coupon_location == 1 && isset($input["lng"])) {
            $city = getCityByLat($input["lng"], $input["lat"], $this->_uniacid);
            $city = array_values($city);
            $where[] = ["a.admin_id", "=", 0];
            $where[] = ["c.title", "in", $city];
        }
        $map[] = ["time_limit", "=", 1];
        $map[] = ["end_time", ">", time()];
        $data = \think\facade\Db::name("massage_service_coupon")->alias("a")->join("shequshop_school_admin b", "a.admin_id = b.id", "left")->join("massage_service_city_list c", "b.city_id = c.id", "left")->where($dis)->where(function ($query) use ($where) {
            $query->whereOr($where);
        })->where(function ($query) use ($map) {
            $query->whereOr($map);
        })->field("a.id,a.title,a.user_limit,a.full,a.discount")->group("a.id")->select()->toArray();
        $coupon_get_type = getConfigSetting($this->_uniacid, "coupon_get_type");
        $arr["discount"] = 0;
        $arr["list"] = $data;
        if ($coupon_get_type == 1) {
            $arr["discount"] = !empty($data) ? array_sum(array_column($data, "discount")) : 0;
            $key = "autoGetCouponautoGetCoupon" . $this->getUserId();
            incCache($key, 1, $this->_uniacid);
            if (getCache($key, $this->_uniacid) == 1 && !empty($data)) {
                foreach ($data as $value) {
                    $coupon_record_model->recordAdd($value["id"], $this->getUserId());
                }
            }
            decCache($key, 1, $this->_uniacid);
        }
        return $this->success($arr);
    }

    public function userGetCoupon()
    {
        $input = $this->_input;
        $coupon_record_model = new \app\massage\model\CouponRecord();
        $coupon_model = new \app\massage\model\Coupon();
        if (!empty($input["coupon_id"])) {
            foreach ($input["coupon_id"] as $value) {
                $dis = ["coupon_id" => $value, "user_id" => $this->getUserId()];
                $find = $coupon_record_model->dataInfo($dis);
                if (!empty($find)) {
                } else {
                    $dis = ["status" => 1, "uniacid" => $this->_uniacid, "send_type" => 2, "id" => $value];
                    $coupon = $coupon_model->dataInfo($dis);
                    if (!empty($coupon)) {
                        $coupon_record_model->recordAdd($value, $this->getUserId());
                    }
                }
            }
        }
        return $this->success(true);
    }

    public function plugAuth()
    {
        $data = \longbingcore\permissions\AdminMenu::getAuthList((int)$this->_uniacid, ["dynamic", "recommend", "store", "map"]);
        $config_model = new \app\massage\model\MassageConfig();
        $config = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
        $data["dynamic"] = !empty($config["dynamic_status"]) ? $data["dynamic"] : 0;
        return $this->success($data);
    }

    public function serviceCateList()
    {
        $cate_model = new \app\massage\model\CateList();
        $dis = ["uniacid" => $this->_uniacid, "status" => 1];
        $data = $cate_model->where($dis)->field("title,id,cover")->order("top desc,id desc")->select()->toArray();
        return $this->success($data);
    }

    public function expectationCityCheck()
    {
        $input = $this->_param;
        $list_model = new \app\massage\model\ExpectationCityList();
        $info_model = new \app\massage\model\ExpectationCityInfo();
        $data["num"] = $info_model->where(["city" => $input["city"], "uniacid" => $this->_uniacid])->count();
        if ($this->getUserId()) {
            $admin_where = ["user_id" => $this->getUserId(), "status" => 1];
            $admin_model = new \app\massage\model\Admin();
            $admin_user = $admin_model->dataInfo($admin_where);
        }
        $data["is_admin"] = !empty($admin_user) ? 1 : 0;
        return $this->success($data);
    }

    public function expectationCity()
    {
        $input = $this->_param;
        $list_model = new \app\massage\model\ExpectationCityList();
        $info_model = new \app\massage\model\ExpectationCityInfo();
        $dis = ["user_id" => $this->_user["id"], "city" => $input["city"]];
        $finds = $info_model->dataInfo($dis);
        if (!empty($finds)) {
            $this->errorMsg("您已投过票，不可重复提交");
        }
        $dis = ["uniacid" => $this->_uniacid, "city" => $input["city"]];
        $find = $list_model->dataInfo($dis);
        \think\facade\Db::startTrans();
        if (empty($find)) {
            $dis["num"] = 1;
            $list_model->dataAdd($dis);
        } else {
            $dis["num"] = $find["num"] + 1;
            $list_model->dataUpdate(["id" => $find["id"]], $dis);
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->_user["id"], "city" => $input["city"]];
        $res = $info_model->dataAdd($insert);
        \think\facade\Db::commit();
        return $this->success($res);
    }

    public function WatermarkImgInfo()
    {
        $input = $this->_param;
        $watermark_model = new \app\massage\model\WatermarkList();
        $dis = ["coach_id" => $input["coach_id"], "type" => $input["type"]];
        $res = $watermark_model->where($dis)->field("watermark_img,original_img")->select()->toArray();
        $data = [];
        if (!empty($res)) {
            foreach ($res as $k => $v) {
                $data[$k]["img"] = !empty($v["watermark_img"]) ? $v["watermark_img"] : $v["original_img"];
            }
        }
        return $this->success($data);
    }

    public function adminCarCashInfo()
    {
        $input = $this->_param;
        $config_model = new \app\massage\model\AdminConfig();
        $admin_id = $input["admin_id"];
        $dis = ["uniacid" => $this->_uniacid, "admin_id" => $admin_id];
        $data = $config_model->dataInfo($dis);
        return $this->success($data);
    }

    public function getServiceObjList()
    {
        $input = $this->_param;
        $city_model = new \app\massage\model\City();
        $lat = $input["lat"];
        $lng = $input["lng"];
        $alh = "ACOS(SIN((" . $lat . " * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((" . $lat . " * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((" . $lng . " * 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6378.137*1000 as distance";
        if (!empty($input["city_id"])) {
            $city_id = $input["city_id"];
        } else {
            $dis[] = ["uniacid", "=", $this->_uniacid];
            $dis[] = ["status", "=", 1];
            $mapor = ["city_type" => 1, "is_city" => 1];
            $key_find = getConfigSetting($this->_uniacid, "index_city_find");
            if ($key_find == 0) {
                $city_id = $city_model->where($dis)->field(["id", $alh])->order("distance asc,id desc")->find();
            } else {
                $city = $this->getLatData($lng, $lat, $this->_uniacid);
                $city_id = $city_model->where("title", "in", $city)->where($dis)->where(function ($query) use ($mapor) {
                    $query->whereOr($mapor);
                })->field(["id", $alh])->order("city_type desc,distance asc,id desc")->find();
            }
            $city_id = !empty($city_id) ? $city_id->id : 0;
        }
        $where[] = ["uniacid", "=", $this->_uniacid];
        $where[] = ["status", "=", 2];
        $where[] = ["auth_status", "=", 2];
        $where[] = ["is_work", "=", 1];
        $where[] = ["city_id", "=", $city_id];
        $where[] = ["industry_type", "=", $input["industry_type"]];
        $collect = [];
        if ($this->getUserId()) {
            $shield_coach = $this->coach_model->getShieldCoach($this->getUserId());
            if (!empty($shield_coach)) {
                $where[] = ["id", "not in", $shield_coach];
            }
            $collect_model = new \app\massage\model\CoachCollect();
            $collect = $collect_model->where(["user_id" => $this->getUserId()])->column("coach_id");
        }
        $coach_model = new \app\massage\model\Coach();
        $station_icon_model = new \app\massage\model\StationIcon();
        $data = $coach_model->where($where)->field(["id,start_time,end_time,store_id,admin_id,coach_name,work_img,star,city_id,is_work,index_top,(order_num+total_order_num) as order_count,station_icon", $alh])->order("distance asc,id desc")->paginate(10)->toArray();
        $config_model = new \app\massage\model\Config();
        $store_model = new \app\massage\model\StoreList();
        $config = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
        if (!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                if (!empty($v["station_icon"])) {
                    $v["station_icon_name"] = $station_icon_model->where(["id" => $v["station_icon"], "status" => 1])->value("title");
                } else {
                    $v["station_icon_name"] = "";
                }
                $v["is_collect"] = in_array($v["id"], $collect) ? 1 : 0;
                $v["near_time"] = $coach_model->getCoachEarliestTimev3($v, $config, 0, 0, 1);
                $v["store"] = $store_model->where(["id" => $v["store_id"], "status" => 1])->field("id,title")->find();
            }
        }
        $data["city_id"] = $city_id;
        return $this->success($data);
    }

    public function cityData()
    {
        if ($this->getUserId()) {
            $address_model = new \app\massage\model\Address();
            $data["address"] = $address_model->where(["user_id" => $this->getUserId()])->order("status desc,id desc")->limit(2)->select()->toArray();
        }
        $city_model = new \app\massage\model\City();
        $mapor = ["city_type" => 1, "is_city" => 1];
        $data["city_list"] = $city_model->where(["uniacid" => $this->_uniacid, "status" => 1, "is_hot" => 1])->where(function ($query) use ($mapor) {
            $query->whereOr($mapor);
        })->field("id,title,lng,lat")->order("id desc")->limit(8)->select()->toArray();
        $hot_count = count($data["city_list"]);
        $total_count = $city_model->where(["uniacid" => $this->_uniacid, "status" => 1])->where(function ($query) use ($mapor) {
            $query->whereOr($mapor);
        })->count();
        $data["have_more"] = $hot_count < $total_count ? 1 : 0;
        return $this->success($data);
    }

    public function getCityList()
    {
        $city_model = new \app\massage\model\City();
        $mapor = ["city_type" => 1, "is_city" => 1];
        $data = $city_model->where(["uniacid" => $this->_uniacid, "status" => 1])->where(function ($query) use ($mapor) {
            $query->whereOr($mapor);
        })->field("id,title,lng,lat")->order("id desc")->select()->toArray();
        return $this->success($data);
    }



}

?>